<template>
  <div class="tree-item" :class="{ 'has-children': hasChildren }">
    <!-- 连接线系统 -->
    <div v-if="level > 0" class="connection-lines">
      <!-- 垂直主线 -->
      <div class="vertical-main-line"></div>
      <!-- 水平连接线 -->
      <div class="horizontal-connect-line"></div>
      <!-- 连接点 -->
      <div class="connection-dot"></div>
    </div>

    <!-- 当前节点卡片 -->
    <div class="node-card" :style="{ marginLeft: `${level * 32}px` }">
      <!-- 展开按钮 -->
      <div class="expand-section">
        <button
          v-if="hasChildren"
          class="expand-btn"
          @click="toggleExpand"
          :aria-label="isExpanded ? '收起' : '展开'"
        >
          <div class="expand-icon" :class="{ expanded: isExpanded }">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3">
              <path d="M9 18l6-6-6-6"/>
            </svg>
          </div>
        </button>
        <div v-else class="expand-placeholder">
          <div class="dot"></div>
        </div>
      </div>

      <!-- 节点内容区域 -->
      <div class="content-section">
        <slot name="content" :row="data" :level="level" :index="index"></slot>
      </div>
    </div>

    <!-- 子节点容器 -->
    <Transition name="expand" appear>
      <div v-if="isExpanded && hasChildren" class="children-wrapper">
        <!-- 子节点垂直连接线 -->
        <div class="children-vertical-line"></div>

        <TreeItem
          v-for="(child, childIndex) in children"
          :key="child.id || childIndex"
          :data="child"
          :tree-props="treeProps"
          :level="level + 1"
          :index="childIndex"
          :is-last="childIndex === children.length - 1"
          @toggle="$emit('toggle', $event)"
        >
          <template #content="slotProps">
            <slot name="content" v-bind="slotProps"></slot>
          </template>
        </TreeItem>
      </div>
    </Transition>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

defineOptions({
  name: 'TreeItem'
})

const props = defineProps({
  data: {
    type: Object,
    required: true
  },
  treeProps: {
    type: Object,
    required: true
  },
  level: {
    type: Number,
    default: 0
  },
  index: {
    type: Number,
    default: 0
  },
  isLast: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['toggle'])

const isExpanded = ref(false)

const children = computed(() => {
  const childrenKey = props.treeProps.children || 'children'
  return props.data[childrenKey] || []
})

const hasChildren = computed(() => {
  return children.value && children.value.length > 0
})

const toggleExpand = () => {
  isExpanded.value = !isExpanded.value
  emit('toggle', props.data, isExpanded.value)
}
</script>

<style lang="scss" scoped>
.tree-item {
  position: relative;
  margin-bottom: 4px;

  // 连接线系统
  .connection-lines {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 32px;
    pointer-events: none;
    z-index: 1;

    .vertical-main-line {
      position: absolute;
      left: 15px;
      top: -12px;
      bottom: 50%;
      width: 2px;
      background: #007AFF;
      border-radius: 1px;
    }

    .horizontal-connect-line {
      position: absolute;
      left: 15px;
      top: 50%;
      width: 17px;
      height: 2px;
      background: #007AFF;
      border-radius: 1px;
      transform: translateY(-50%);
    }

    .connection-dot {
      position: absolute;
      left: 13px;
      top: 50%;
      width: 6px;
      height: 6px;
      background: #007AFF;
      border-radius: 50%;
      transform: translateY(-50%);
      box-shadow: 0 0 0 2px #ffffff;
    }
  }

  // 节点卡片
  .node-card {
    background: #ffffff;
    border-radius: 16px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    border: 1px solid #E5E5EA;
    display: flex;
    align-items: center;
    min-height: 64px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    z-index: 2;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
      transform: translateY(-1px);
      border-color: #007AFF;
    }

    &:active {
      transform: translateY(0) scale(0.98);
    }
  }

  // 展开按钮区域
  .expand-section {
    padding: 16px;
    display: flex;
    align-items: center;
    justify-content: center;

    .expand-btn {
      width: 36px;
      height: 36px;
      border: none;
      background: #007AFF;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
      box-shadow: 0 2px 8px rgba(0, 122, 255, 0.3);

      &:hover {
        transform: scale(1.1);
        box-shadow: 0 4px 12px rgba(0, 122, 255, 0.4);
        background: #0056CC;
      }

      &:active {
        transform: scale(0.95);
      }

      .expand-icon {
        width: 16px;
        height: 16px;
        color: #ffffff;
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);

        &.expanded {
          transform: rotate(90deg);
        }

        svg {
          width: 100%;
          height: 100%;
        }
      }
    }

    .expand-placeholder {
      width: 36px;
      height: 36px;
      display: flex;
      align-items: center;
      justify-content: center;

      .dot {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        background: #C7C7CC;
      }
    }
  }

  // 内容区域
  .content-section {
    flex: 1;
    min-width: 0;
    padding: 16px 20px 16px 0;

    // 确保内容区域内的元素对齐
    .menu-card {
      width: 100%;

      .menu-actions {
        // 重置任何继承的边距，确保按钮对齐
        margin-left: 0 !important;
        margin-right: 0 !important;

        .action-btn {
          // 确保按钮完全填充可用宽度
          width: 100% !important;
          box-sizing: border-box;
        }
      }
    }
  }

  // 子节点容器
  .children-wrapper {
    position: relative;
    margin-top: 4px;

    .children-vertical-line {
      position: absolute;
      left: 31px;
      top: 0;
      width: 2px;
      background: #007AFF;
      border-radius: 1px;
      // 动态计算高度，连接到最后一个子节点的中心
      height: calc(100% - 32px);
    }
  }

  // 最后一个子节点的特殊处理
  &:last-child {
    .connection-lines .vertical-main-line {
      // 最后一个节点的垂直线只到节点中心
      bottom: 50%;
    }
  }

  // 为父节点添加延伸线到子节点区域
  &.has-children {
    .connection-lines .vertical-main-line {
      // 有子节点时，垂直线延伸到子节点区域
      bottom: -4px;
    }
  }
}

// 展开动画
.expand-enter-active,
.expand-leave-active {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
}

.expand-enter-from {
  opacity: 0;
  transform: translateY(-8px);
  max-height: 0;
}

.expand-leave-to {
  opacity: 0;
  transform: translateY(-8px);
  max-height: 0;
}

.expand-enter-to,
.expand-leave-from {
  opacity: 1;
  transform: translateY(0);
  max-height: 1000px;
}

// 移动端优化
@media (max-width: 768px) {
  .tree-item {
    .node-card {
      border-radius: 20px;
      min-height: 80px;
    }

    .expand-section {
      padding: 20px;

      .expand-btn {
        width: 44px;
        height: 44px;
        box-shadow: 0 3px 12px rgba(0, 122, 255, 0.4);

        &:hover {
          transform: scale(1.05);
          box-shadow: 0 6px 16px rgba(0, 122, 255, 0.5);
        }

        .expand-icon {
          width: 20px;
          height: 20px;
        }
      }

      .expand-placeholder {
        width: 44px;
        height: 44px;

        .dot {
          width: 10px;
          height: 10px;
        }
      }
    }

    .content-section {
      padding: 20px 24px 20px 0;

      // 移动端按钮对齐优化
      .menu-card {
        .menu-actions {
          margin-left: 0 !important;
          margin-right: 0 !important;

          .action-btn {
            width: 100% !important;
            margin: 0 !important;
          }
        }
      }
    }

    .connection-lines {
      width: 40px;

      .vertical-main-line {
        left: 19px;
        width: 3px;
      }

      .horizontal-connect-line {
        left: 19px;
        width: 21px;
        height: 3px;
      }

      .connection-dot {
        left: 16px;
        width: 8px;
        height: 8px;
      }
    }

    .children-wrapper {
      .children-vertical-line {
        left: 39px;
        width: 3px;
        height: calc(100% - 40px);
      }
    }

    // 移动端最后一个子节点处理
    &:last-child {
      .connection-lines .vertical-main-line {
        bottom: 50%;
      }
    }
  }
}
</style>
